﻿Imports System.ComponentModel
Imports DevExpress.Skins
Imports DevExpress.LookAndFeel
Imports DevExpress.UserSkins
Imports DevExpress.XtraBars
Imports DevExpress.XtraBars.Ribbon
Imports DevExpress.XtraBars.Helpers
Imports DevExpress.XtraSplashScreen
Imports DevExpress.XtraEditors
Imports DevExpress.XtraGrid.Views.Base
Imports DevExpress.Data
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid

Public Class DM_HangHoa
    Dim Ma As String
    Dim State As Integer = 0 'trạng thái form '0 mặc định'1 tạo mới'2 sửa '3 xoa
    Private flag As Boolean = False 'tránh loop xtragrid
    Private Sub setReadonlyControl(ByVal b As Boolean)
        setReadonly(Me.layoutControl, b)
        'If b Then
        '    Me.IGridViewTienChiTiet.OptionsBehavior.Editable = False
        'Else
        '    Me.IGridViewTienChiTiet.OptionsBehavior.Editable = True
        'End If
        'If b Then 'trong trạng thái readonly  thì không cho nhập và dòng trống thêm ở cuối
        '    Me.IGridViewTienChiTiet.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        '    Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        'Else  'cho thêm dòng trống ở cuối (khác form đơn - bỏ dòng : And State = 1)
        '    Me.IGridViewTienChiTiet.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.Bottom
        '    Me.IGridViewThue.OptionsView.NewItemRowPosition = DevExpress.XtraGrid.Views.Grid.NewItemRowPosition.None
        'End If
        If b Then
            'dxprovidervalidation
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Manual
            setButtonTrangThaiSua(False)
        Else
            Me.DxValidationProvider1.ValidationMode = DXErrorProvider.ValidationMode.Auto
            setButtonTrangThaiSua(True)
        End If
    End Sub
    Private Sub setButtonTrangThaiSua(ByVal b As Boolean) 'set nút lưu và xóa - khi ở trạng thái sửa thì mới cho phép thao tác trên nó (enable = true) - các trạng thái khác thì không cho (enable = false)
        Me.BarButtonItemLuu.Enabled = b
        Me.BarButtonItemXoa.Enabled = b
        If b Then 'xử lý nút sửa (nếu nút lưu - xóa cho phép thì sửa ko cho phép và ngược lại)
            Me.BarButtonItemSua.Enabled = False
        Else
            Me.BarButtonItemSua.Enabled = True
        End If
        setButtonChiTietTrangThaiSua(b)
    End Sub
    Private Sub setButtonChiTietTrangThaiSua(ByVal b As Boolean)
        Me.BarButtonItemTaoMoiChiTiet.Enabled = b
        Me.BarButtonItemXoaChiTiet.Enabled = b
        Me.BarButtonItemHuyThayDoiChiTiet.Enabled = b
    End Sub
    Private Sub loadDuLieu()
        SplashScreenManager.ShowForm(GetType(WaitForm1))
        Me.DmkhachhangTableAdapter.FillByMaTenDiaChi(Me.PCV_FISHDataSet.dmkhachhang)
        Me.DmnghiepvuTableAdapter.Fill(Me.PCV_FISHDataSet.dmnghiepvu)
        Me.GetPhanLoai_TinhTrangPhieuTableAdapter.Fill(Me.PCV_FISHDataSet.getPhanLoai_TinhTrangPhieu)
        Me.DMTaiKhoanKeToanCap2TableAdapter.Fill(Me.PCV_FISHDataSet.DMTaiKhoanKeToanCap2)
        Me.DmnguonvonTableAdapter.Fill(Me.PCV_FISHDataSet.dmnguonvon)
        Me.DmkheuocvayTableAdapter.Fill(Me.PCV_FISHDataSet.dmkheuocvay)
        Me.DmkhoanmucphiTableAdapter.Fill(Me.PCV_FISHDataSet.dmkhoanmucphi)
        Me.DmnganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmnganhang)
        Me.GetDMTyGia_TyGiaTableAdapter.Fill(Me.PCV_FISHDataSet.getDMTyGia_TyGia, Today)
        Me.DmphongbanTableAdapter.Fill(Me.PCV_FISHDataSet.dmphongban)
        Me.DmtygiaTableAdapter.Fill(Me.PCV_FISHDataSet.dmtygia)
        Me.DmtaikhoannganhangTableAdapter.Fill(Me.PCV_FISHDataSet.dmtaikhoannganhang)
        Me.Dmhanghoa_donvitinhTableAdapter.Fill(Me.PCV_FISHDataSet.dmhanghoa_donvitinh)
        Me.DmhanghoaTableAdapter.Fill(Me.PCV_FISHDataSet.dmhanghoa)
        setReadonlyControl(True)
        Me.TC_TienIGridControl.ForceInitialize()
        SplashScreenManager.CloseForm()
    End Sub
    Private Sub KH_HangHoa_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'PCV_FISHDataSet.DMDonViTinh' table. You can move, or remove it, as needed.
        Me.DMDonViTinhTableAdapter.Fill(Me.PCV_FISHDataSet.DMDonViTinh)
        Me.loadDuLieu()
    End Sub

    Private Sub BarButtonItemTaoMoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoi.ItemClick
        'focus
        Me.setReadonlyControl(False)
        Me.MaITextEdit.Focus()
        Me.State = 1
    End Sub

    Private Sub BarButtonItemLuu_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLuu.ItemClick
        Try
            Me.IGridViewThue.PostEditor()
            Me.IGridViewThue.UpdateCurrentRow()
            Me.IGridViewThue.CloseEditor()
            'Me.IGridViewTienChiTiet.PostEditor()
            'Me.IGridViewTienChiTiet.UpdateCurrentRow()
            'Me.IGridViewTienChiTiet.CloseEditor()
            Me.DmhanghoaBindingSource.EndEdit()
            Me.FKdmhanghoadoma6B24EA82BindingSource.EndEdit()
            If Me.PCV_FISHDataSet.HasChanges Then 'coi có thay đổi dữ liệu hay ko ?
                If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn 'Lưu' không ?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                    Try
                        Me.TableAdapterManager.UpdateAll(Me.PCV_FISHDataSet)
                        Me.State = 0 'trở về trạng thái mặc định 0
                        Me.setReadonlyControl(True) 'set lại readonly
                        setButtonTrangThaiSua(False) 'set lại nút 
                    Catch ex As Exception
                        DevExpress.XtraEditors.XtraMessageBox.Show("Xảy ra lỗi :" & ex.Message, "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
                    End Try
                End If
            Else 'nếu ko thay đổi dữ liệu
                DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu ?", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
                Me.setReadonlyControl(True) 'set lại readonly
                setButtonTrangThaiSua(False) 'set lại nút 
                Me.State = 0
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message) 'tung message khi có ngoại lệ
        End Try
    End Sub

    Private Sub BarButtonItemSua_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemSua.ItemClick
        Me.setReadonlyControl(False)
        Me.State = 2
    End Sub

    Private Sub BarButtonItemThoat_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemThoat.ItemClick
        Me.Dispose() 'giải phóng ram
        Me.Close() 'đóng
    End Sub

    Private Sub BarButtonItemTaoMoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemTaoMoiChiTiet.ItemClick
        'Me.IGridViewTienChiTiet.MoveLast()
        'Me.IGridViewTienChiTiet.Focus()
        'Me.IGridViewTienChiTiet.AddNewRow()
        'Me.IGridViewTienChiTiet.FocusedColumn = Me.IGridViewTienChiTiet.VisibleColumns(0)
        'Me.IGridViewTienChiTiet.ShowEditor()
    End Sub

    Private Sub BarButtonItemFirst_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirst.ItemClick
        Me.DmhanghoaBindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonPrevious_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonPrevious.ItemClick
        Me.DmhanghoaBindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonNext_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonNext.ItemClick
        Me.DmhanghoaBindingSource.MoveNext()
    End Sub

    Private Sub BarButtonLast_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonLast.ItemClick
        Me.DmhanghoaBindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemFirstChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemFirstChiTiet.ItemClick
        Me.FKdmhanghoadoma6B24EA82BindingSource.MoveFirst()
    End Sub

    Private Sub BarButtonItemPreviousChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemPreviousChiTiet.ItemClick
        Me.FKdmhanghoadoma6B24EA82BindingSource.MovePrevious()
    End Sub

    Private Sub BarButtonItemNextChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemNextChiTiet.ItemClick
        Me.FKdmhanghoadoma6B24EA82BindingSource.MoveNext()
    End Sub

    Private Sub BarButtonItemLastChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLastChiTiet.ItemClick
        Me.FKdmhanghoadoma6B24EA82BindingSource.MoveLast()
    End Sub

    Private Sub BarButtonItemXoa_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoa.ItemClick
        If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa số chứng từ '" & Me.MaITextEdit.EditValue & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
            Me.DmhanghoaBindingSource.RemoveCurrent()
            Me.DmhanghoaTableAdapter.Update(Me.PCV_FISHDataSet.dmhanghoa)
            Me.setReadonlyControl(True)
            Me.State = 0 'trở về mặc định
        End If
    End Sub

    Private Sub BarButtonItemHuyThayDoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoi.ItemClick
        Me.DmhanghoaBindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi đang làm không?", "Thông Báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemXoaChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemXoaChiTiet.ItemClick
        'If Me.TabbedControlGroup2.SelectedTabPageIndex = 0 Then
        '    If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn xóa '" & Me.IGridViewTienChiTiet.GetRowCellValue(Me.IGridViewTienChiTiet.FocusedRowHandle, "mahang") & "' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
        '        Me.IGridViewTienChiTiet.DeleteRow(Me.IGridViewTienChiTiet.FocusedRowHandle)
        '    End If
        'End If
    End Sub

    Private Sub BarButtonItemHuyThayDoiChiTiet_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemHuyThayDoiChiTiet.ItemClick
        Me.FKdmhanghoadoma6B24EA82BindingSource.EndEdit()
        If Me.PCV_FISHDataSet.HasChanges Then
            If DevExpress.XtraEditors.XtraMessageBox.Show("Bạn có muốn hủy tất cả những thay đổi của phần 'hàng hóa' không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Information) = Windows.Forms.DialogResult.Yes Then
                Me.PCV_FISHDataSet.RejectChanges()
            End If
        Else
            DevExpress.XtraEditors.XtraMessageBox.Show("Chưa có thay đổi dữ liệu!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information)
        End If
    End Sub

    Private Sub BarButtonItemLamTuoi_ItemClick(ByVal sender As System.Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs) Handles BarButtonItemLamTuoi.ItemClick
        Dim position As Integer = Me.DmhanghoaBindingSource.Position 'load dữ liệu lại nhưng giữ nguyên phiếu
        loadDuLieu()
        Me.DmhanghoaBindingSource.Position = position
    End Sub

End Class
